package packageWA;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class AnadirEmpresa extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtNombre;
	private JTextField txtCif;
	private JTextField txtTelf;
	private JTextField txtEmail;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnadirEmpresa frame = new AnadirEmpresa();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnadirEmpresa() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 435, 306);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblEmpresa = new JLabel("Empresa:");
		lblEmpresa.setBounds(66, 53, 61, 16);
		contentPane.add(lblEmpresa);
		
		JLabel lblCif = new JLabel("CIF:");
		lblCif.setBounds(66, 93, 61, 16);
		contentPane.add(lblCif);
		
		JLabel lblTelfono = new JLabel("Tel\u00E9fono:");
		lblTelfono.setBounds(66, 136, 61, 16);
		contentPane.add(lblTelfono);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(66, 181, 61, 16);
		contentPane.add(lblEmail);
		
		txtNombre = new JTextField();
		txtNombre.setBounds(177, 47, 189, 28);
		contentPane.add(txtNombre);
		txtNombre.setColumns(10);
		
		txtCif = new JTextField();
		txtCif.setColumns(10);
		txtCif.setBounds(177, 87, 189, 28);
		contentPane.add(txtCif);
		
		txtTelf = new JTextField();
		txtTelf.setColumns(10);
		txtTelf.setBounds(177, 130, 189, 28);
		contentPane.add(txtTelf);
		
		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(177, 175, 189, 28);
		contentPane.add(txtEmail);
		
		JButton btnCrearEmpresa = new JButton("Crear Empresa");
		btnCrearEmpresa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Comprobar campos requeridos
				if(txtNombre.getText().isEmpty() || txtCif.getText().isEmpty()){
					JOptionPane.showMessageDialog(null,"Los campos Empresa y CIF son obligatorios");
				}
				else{
					//Crear empresa
					String sql = "INSERT INTO empresa (CIF, Nombre, Telefono, Email) VALUES ('" + txtCif.getText() + "', '" + txtNombre.getText() + "', '" + txtTelf.getText() + "', '" + txtEmail.getText() + "');";
					try {
						GestorBD.getInstance().insertar(sql);
						JOptionPane.showMessageDialog(null,"La empresa " + txtNombre.getText() + " ha sido creada");
						dispose();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		btnCrearEmpresa.setBounds(107, 236, 117, 29);
		contentPane.add(btnCrearEmpresa);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancelar.setBounds(236, 236, 117, 29);
		contentPane.add(btnCancelar);
	}
}
